import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Components from "unplugin-vue-components/vite";
import defineOptions from "unplugin-vue-define-options/vite";
import type { PluginOption } from "vite";
import { defineConfig, loadEnv } from "vite";
import WindiCSS from "vite-plugin-windicss";

import optimizeDepsIncludes from "./optimize-deps.js";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: "/",
    plugins: [
      vue(),
      vueJsx(),
      WindiCSS(),
      defineOptions(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        imports: ["vue", "vue-router", "pinia"],
        dts: "./auto-imports.d.ts",
        eslintrc: {
          enabled: true, // 1、改为true用于生成eslint配置。2、生成后改回false，避免重复生成消耗
        },
      }),
      Components({
        resolvers: [ElementPlusResolver({ importStyle: "sass" })],
      }),
    ] as PluginOption[],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        },
        scss: {
          api: "modern-compiler",
          silenceDeprecations: ["legacy-js-api"],
          // 自动导入定制化样式文件进行样式覆盖
          additionalData: `
            @use "@/styles/element/index.scss" as *;
          `,
        },
      },
    },
    server: {
      host: "0.0.0.0",
      cors: true,
      port: 3000,
      // proxy: {
      //   "/api": {
      //     target: "https://api-test.realboxs.com",
      //     changeOrigin: true,
      //     ws: true,
      //     rewrite: (path) => {
      //       return path.replace(/\/api/, "");
      //     },
      //   },
      // },
    },

    optimizeDeps: {
      include: optimizeDepsIncludes,
    },
    build: {
      sourcemap: true,
      assetsDir: "assets",
      outDir: env.VITE_OUTDIR || "dist",
    },
  };
});
